﻿package CutLoad.cookies
{
	import CutLoad.*;
	import CutLoad.components.loadbar.Loadbar;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	/**
	 * 简单扩展了Loader ，加载可视对象文件
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CLoader extends Base
	{
		
		private var _url:String;
		private var nowW:Number;
		private var nowH:Number;
		//
		private var myLoad:Loader;
		private var loadBar:Loadbar
		
		private var funCompelet:Function=null,funProgress:Function=null,FunError:Function;
		//如果加载错误，重新加载的次数
		private var reLoadCount:Number=3;
		private var reLoadCount_i:Number=0;
		/**
		 *  
		 * @param url 加载地址
		 * @param w 宽度 默认设置为0，加载完好，显示原始宽
		 * @param h 高度 默认设置为0，加载完好，显示原始高
		 * @param loadBarTxtColor 进度指示文字色
		 * 
		 */		
		public function CLoader(url:String,w:Number=0,h:Number=0,loadBarTxtColor:Number=0x000000,is_loadBar_visible:Boolean=true)
		{
			this
			this._url=url;
			this.nowW=w;
			this.nowH=h;
			//
			loadBar=Loadbar.getOnlyTxtLoadBar(w,h,10,loadBarTxtColor)
			loadBar.setXY(0,0,this);
			loadBar.visible=is_loadBar_visible;
			//
			myLoad=new Loader();
			myLoad.contentLoaderInfo.addEventListener(Event.COMPLETE,cmp);
			myLoad.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progress);
			myLoad.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,error);
			myLoad.load(new URLRequest(url));
		}
		/**
		 * 获得内部 Loader对象 
		 * @return 
		 * 
		 */		
		public function getLoader():Loader{
			return myLoad;
		}
		/**
		 * 添加遮蔽，如果加载的东西大于指定宽度，超过的地方示不会显示 
		 * 
		 */		
		public function setMask():void{
			var baseMask:Sprite=Painter.getRect(this.getShowWidth(),this.getShowHeight());
			this.addChild(baseMask);
			myLoad.mask=baseMask
		}
		/**
		 * 设置完成时要做的处理 :格式:function(loader:Loader)  注：loader为Loader对象
		 * @param fun 
		 * 
		 */		
		public function setFunCompelet(fun:Function):void{
			this.funCompelet=fun;
			
		}
		/**
		 * 设置加载失败要做的处理
		 * @param fun 设置加载失败要做的处理:格式 function(me:CLoader) 或者  function();
		 * @param reLoadCount
		 * 
		 */		
		public function setFunError(fun:Function,reLoadCount:Number):void{
			this.FunError=fun;
			this.reLoadCount=reLoadCount;
		}
		/**
		 * 设置加载过程返回百份比处理方法:格式:function(pre:Numbe)  注：pre为百分比
		 * @param fun
		 * 
		 */		
		public function setFunProgress(fun:Function):void{
			funProgress=fun;
		}
		/**
		 * 返回默认的加载进度条
		 * @return 
		 * 
		 */		
		public function getLoadBar():Loadbar{
			return loadBar;
		}
		/**
		 * 加载的路经 
		 * @return 
		 * 
		 */		
		public function get url():String{
			return _url;
		}
		private function error(e:IOErrorEvent):void{
			//myLoad.load(new URLRequest(_url));
			reLoadCount_i++;
			if(reLoadCount_i>=this.reLoadCount){
				//如果加载这么多次，都没有成功，就说明加载失败。
				try{
					FunError(this);
				}catch(e:ArgumentError){
					FunError();
				}
			}else{
				//
				myLoad.load(new URLRequest(_url));
			}
			//throw new Error("没有找到 "+url+" 这个文件!");
		}
		private function progress(e:ProgressEvent):void{
			var pre:Number=Math.floor((e.bytesLoaded/e.bytesTotal)*100)/100;
			loadBar.setValue(pre,"");
			loadBar.dreawing();
			if(funProgress!=null) funProgress(pre);
		}
		private function cmp(e:Event):void{
			loadBar.killMe();
			this.addChild(myLoad);
			if(this.nowW!=0) myLoad.width=this.nowW;
			if(this.nowH!=0) myLoad.height=this.nowH;		
			//
			if(funCompelet!=null) funCompelet(myLoad);
		}
		//覆盖---------------------
		override public function killMe():void{
			if(myLoad!=null){
				if(myLoad.contentLoaderInfo.hasEventListener(Event.COMPLETE)) myLoad.contentLoaderInfo.removeEventListener(Event.COMPLETE,cmp);
				if(myLoad.contentLoaderInfo.hasEventListener(ProgressEvent.PROGRESS)) myLoad.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progress);
				if(myLoad.contentLoaderInfo.hasEventListener(IOErrorEvent.IO_ERROR)) myLoad.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,error);
				myLoad.unload();
				if(myLoad.parent!=null) this.removeChild(myLoad);
			}
			super.killMe();
		}
		/**
		 * 获得宽度 
		 * @return 
		 * 
		 */		
		override public function getShowWidth():Number{
			return this.nowW
		}
		/**
		 * 获得高度 
		 * @return 
		 * 
		 */		
		override public function getShowHeight():Number{
			return this.nowH;	
		}
		/**
		 * 设置宽，高度 
		 * @param w
		 * @param h
		 * 
		 */		
		override public function setWH(w:Number=0, h:Number=0):void{
			this.nowH=h;
			this.nowW=h;
			if(this.nowW!=0) myLoad.width=this.nowW;
			if(this.nowH!=0) myLoad.height=this.nowH;
		}
	}
}